<?php


namespace app\api\controller;

use app\api\model\Circle as CircleModel;
use app\api\model\Posts;
use app\api\model\User;

class Circle extends BaseController
{
    /**
     * 获取到全部圈子
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getCircleAll()
    {
        $model = new  CircleModel();
        $list = $model->getCircle();
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 圈子详情
     *
     * @return array|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function circleDetail()
    {
        $id = $this->request->param();
        $id = 1;
        $details = new CircleModel();
        $details = $details->detail($id);
        return $this->renderSuccess(compact('details'));
    }

    /**
     *  根据关键词搜索出圈子
     */
    public function searchCircle()
    {
        //获取搜索框内的关键字
        $data = $this->request->param();
        $data = '圈子';
        $search = new CircleModel();
        $searchcircle = $search->searchcir($data);
        return $this->renderSuccess(compact('searchcircle'));
    }

    /**
     * 点击某圈子看到该圈子下所有帖子
     *
     */
    public function getCpost()
    {
        //获取点击的圈子id
        $circle_id = $this->request->param();
        $circle_id = 4;
        $cposts = new Posts();
        $cpost = $cposts->getcPost($circle_id);
        return $this->renderSuccess(compact('cpost'));
    }

    /**
     *
     * 获取推荐的帖子
     *
     */
    public function recPosts()
    {

        // $postm = new Posts();
        // $postlist = $postm->recPosts();
        $type = 1;

        $list = new Posts();
        $postlist = $list->recPosts();
        return $this->renderSuccess(compact('postlist'));
        // return json($list);
    }

    /**
     * 帖子点赞
     *
     * 帖子id $post_id
     * 登录用户id  $user_id
     */
    public function thumbs()
    {
        $post_id = $this->request->param('$post_id');
        $post_id = 1;
        $zancount = Posts::where('id', $post_id)->field('zan')->find()->toArray();
        $zan = $zancount['zan'] + 1;
        $addzan = Posts::where('id', $post_id)->update(['zan' => $zan]);
        return $this->renderSuccess(compact('addzan'));
    }

    // /**
    //  *  帖子评论数
    //  *
    //  * 帖子id   $post_id
    //  */
    // public function comment()
    // {
    //     $post_id = 1;
    //     $postlist = Posts::with('pinglun')->where('id', $post_id)->find();
    //     $count = $postlist['comment_num'];
    //     // var_dump($postlist);
    //     var_dump(json_encode($postlist['pinglun']));
    //     // var_dump(json_encode($postlist));
    //
    // }
    // /**
    //  * 帖子取消点赞
    //  *
    //  * 帖子id $post_id
    //  * 登录用户id  $user_id
    //  */
    // public function delthumbs()
    // {
    //     $post_id = $this->request->param('$post_id');
    //     $post_id = 1;
    //     $zancount = Posts::where('id', $post_id)->field('zan')->find()->toArray();
    //     $zan = $zancount['zan'] - 1;
    //     $addzan = Posts::where('id', $post_id)->update(['zan' => $zan]);
    //     return $this->renderSuccess(compact('addzan'));
    // }


    /**
     * 获取指定帖子详情
     *
     * @return array|\think\response\Json
     */
    public function postsDetail()
    {
        $id = $this->request->param();
        $id = 2;
        $search = new Posts();
        $searchposts = $search->detail($id);
        return $this->renderSuccess(compact('searchposts'));
    }

    /**
     * 根据帖子关注用户
     *
     * 当前登录id  $user_id
     * 帖子用户id  $postuser_id
     */
    public function clickFollow()
    {
        $user_id = Session('user_id');
        $user_id = 7;
        //获取到该发帖人id
        $postuser_id = $this->request->param();
        $postuser_id = 1;
        //查询出发帖人的粉丝数   点击关注后发帖人粉丝数量加1  并且将当前登录用户id加入发帖人粉丝总id中
        $fanscounts = User::where('id', $postuser_id)->field('fans_num')->find()->toArray();
        $gcounts = User::where('id', $user_id)->field('guanzhu_num')->find()->toArray();
        $fanscount = $fanscounts['fans_num'] + 1;
        $gcount = $gcounts['guanzhu_num'] + 1;
        //查询出当前用户的关注数 点击关注后当前用户关注数加1 并且将发帖用户id加入发帖人关注总id中
        User::where('id', $postuser_id)->update(['fans_num' => $fanscount]);
        User::where('id', $user_id)->update(['guanzhu_num' => $gcount]);
        return $this->renderSuccess(compact(''), '关注成功');
    
    }


    /**
     * 根据关键词搜索出帖子
     *
     */
    public function searchPosts()
    {
        //获取搜索框内的关键字
        $data = $this->request->param();
        $data = '测试帖子';
        $search = new Posts();
        $searchposts = $search->searchPost($data);
        return $this->renderSuccess(compact('searchposts'));
    }

    /**
     * 发帖
     *
     */
    public function sendPost()
    {
        $user_id = session('user_id');
        $data = $this->request->param();
        $user_id = 6;
        $data['user_id'] = $user_id;
        $sendpost = new Posts();
        $sendposts = $sendpost->sendPost($data);
        return $this->renderSuccess(compact('sendposts'));
    }

    /**
     * 发帖时选择圈子
     *
     */


    /**
     * 帖子转发
     *
     * 帖子id $post_id
     */
    public function forwardPost()
    {
        $post_id = $this->request->param();
        $post_id = 2;
        $postinfo = Posts::where('id', $post_id)->find()->toArray();
        return $postinfo;
    }

    /**
     * 帖子转发 发送
     *
     * 登录用户id  $user_id
     * 帖子id     $post_id
     */
    public function sucforwardPost()
    {
        $res = $this->forwardPost();

        $user_id = $this->request->param();
        $user_id = 3;
        $res = [
            'circle_id' => $res['circle_id'],
            'user_id' => $user_id,
            'title' => $res['title'],
            'content' => $res['content'],
            'recommend' => $res['recommend'],
        ];
        $forward = Posts::insert($res);
        return $this->renderSuccess(compact('forward'));
    }
}